﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.项目流程.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.项目流程
{
    public partial class Form添加任务 : Form
    {
        private String id { get; set; }
        private String name { get; set; }
        public WBSItemsDTO WBSItemsDTO { get; set; }
        public Form添加任务(String id, String parentId)
        {
            //传参后new一个对象
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            this.textBox任务id2.Text = id;
            this.textBox父任务id2.Text = parentId;
            this.WBSItemsDTO = new WBSItemsDTO(id, parentId);
            //给searchLookUpEdit绑定数据
            this.searchLookUpEdit选择员工.Properties.DataSource = 任务Service.getAllStaffs();

        }
        //不传参
        public Form添加任务()
        {
            InitializeComponent();
            
        }
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        //searchlookup添加
        private void searchLookUpEdit选择员工_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn选择员工_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit选择员工View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit选择员工.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.id = this.searchLookUpEdit选择员工.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                this.name = this.searchLookUpEdit选择员工.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
            }           
        }
        /// <summary>
        /// 清楚按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn选择员工_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit选择员工.ToolTip = null;
            searchLookUpEdit选择员工.EditValue = null;
        }
        /// <summary>
        /// 选完后显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit选择员工_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.name;
        }

        private void simpleButton添加_Click(object sender, EventArgs e)
        {
            //调用build
            new WBSItemsDTO().build(
                this.textBox任务描述2.Text.Trim(),
                this.textBoxCode2.Text.Trim(),
                name
                );
            //this.Close();
        }
    }
}
